<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="参保人" prop="insuredName">
        <el-input
          v-model="queryParams.insuredName"
          placeholder="请输入参保人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="identificationCard">
        <el-input
          v-model="queryParams.identificationCard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable>
          <el-option label="待审核" value="0" />
          <el-option label="已通知" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="paymentList">
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column label="参保人" align="center" prop="insuredName" />
      <el-table-column label="身份证号" align="center" prop="identificationCard" :show-overflow-tooltip="true" />
      <el-table-column label="支付金额" align="center" prop="paymentAmount" />
      <el-table-column label="医疗机构" align="center" prop="hospitalName" />
      <el-table-column label="就诊日期" align="center" prop="visitDate" />
      <el-table-column label="报销金额" align="center" prop="reimbursementAmount" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.status === '0'">待审核</el-tag>
          <el-tag type="success" v-else-if="scope.row.status === '1'">已通知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-message"
            @click="handleNotify(scope.row)"
            :disabled="scope.row.status === '1'"
          >通知医保局</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
          >查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情对话框 -->
    <el-dialog title="支付详情" :visible.sync="detailOpen" width="600px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="参保人">{{ detail.insuredName }}</el-descriptions-item>
        <el-descriptions-item label="身份证号">{{ detail.identificationCard }}</el-descriptions-item>
        <el-descriptions-item label="医疗机构">{{ detail.hospitalName }}</el-descriptions-item>
        <el-descriptions-item label="就诊日期">{{ detail.visitDate }}</el-descriptions-item>
        <el-descriptions-item label="门诊/住院">{{ detail.visitType === '1' ? '门诊' : '住院' }}</el-descriptions-item>
        <el-descriptions-item label="医疗总费用">{{ detail.paymentAmount }} 元</el-descriptions-item>
        <el-descriptions-item label="自付金额">{{ detail.selfPayAmount }} 元</el-descriptions-item>
        <el-descriptions-item label="报销金额">{{ detail.reimbursementAmount }} 元</el-descriptions-item>
        <el-descriptions-item label="报销比例" :span="2">{{ detail.reimbursementRatio }} %</el-descriptions-item>
        <el-descriptions-item label="诊断信息" :span="2">{{ detail.diagnosis || '无' }}</el-descriptions-item>
        <el-descriptions-item label="备注信息" :span="2">{{ detail.remark || '无' }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listAuditBureau, notifyInsuranceBureau} from "@/api/medical/auditbureau";
export default {
  name: "AuditBureau",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参保人员支付信息表格数据
      paymentList: [],
      // 详情对话框
      detailOpen: false,
      // 详情数据
      detail: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        insuredName: undefined,
        identificationCard: undefined,
        status: undefined
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参保人员支付列表 */
    async getList() {
      this.loading = true;
      const res = await listAuditBureau(this.queryParams)
      if (res.rows) {
        // 将后端返回的AuditDepartInfo对象转换为前端使用的格式
        this.paymentList = res.rows.map(item => {
          return {
            id: item.id,
            insuredName: item.insuredPersons ? item.insuredPersons.name : '',
            identificationCard: item.identificationCard,
            paymentAmount: item.payMoney,
            hospitalName: item.medicalInstitution,
            visitDate: item.visitTime,
            reimbursementAmount: item.reimbursementAmount,
            status: item.status ? item.status.toString() : '0',
            
            // 保存完整对象用于详情展示
            insuredPersonId: item.insuredPersonId,
            insuredPersons: item.insuredPersons,
            visitType: item.visitType || '1',  // 默认门诊
            totalAmount: item.totalAmount || item.payMoney,
            selfPayAmount: item.selfPayAmount || 0,
            reimbursementRatio: item.reimbursementRatio || ((item.reimbursementAmount / item.payMoney) * 100).toFixed(0),
            diagnosis: item.diagnosis || '',
            remark: item.remark || ''
          }
        }) || []
        this.total = res.total || this.paymentList.length
      } else {
        this.paymentList = []
        this.total = 0
      }
      this.loading = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查看详情按钮操作 */
    handleDetail(row) {
      this.detail = row;
      this.detailOpen = true;
    },
    /** 通知医保局按钮操作 */
    handleNotify(row) {
      this.$confirm('确认要通知医保局财务处关于 ' + row.insuredName + ' 的报销信息吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        this.loading = true;
        try {
          await notifyInsuranceBureau(row.id);
          // 更新状态
          row.status = '1';
          this.$message({
            type: "success",
            message: "通知成功"
          });
        } catch (error) {
          this.$message({
            type: "error",
            message: "通知失败: " + (error.message || '未知错误')
          });
        } finally {
          this.loading = false;
        }
      }).catch(() => {});
    }
  }
};
</script> 